var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data:{
    orderList: [],
    page: 1,
    size: 10,
    loadmoreText: '正在加载更多数据',
    nomoreText: '全部加载完成',
    nomore: false,
    totalPages: 1
  },
  //页面初始化 options为页面跳转所带来的参数
  onLoad:function(options){},
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    this.getOrderList()
  },
  //得到订单列表
  getOrderList(){
    wx.showLoading({
      title: '加载中...',
      success: function () { }
    });
    let that = this;
    if (that.data.totalPages <= that.data.page - 1) {
      that.setData({
        nomore: true
      })
      return;
    }
    util.request(api.OrderList, {page: that.data.page, size: that.data.size}).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          orderList: that.data.orderList.concat(res.data.data),
          page: res.data.currentPage + 1,
          totalPages: res.data.totalPages
        });
        wx.hideLoading();
      }
    });
  },
  //去付款
  payOrder(e){
    wx.redirectTo({
      url: '/pages/pay/pay?orderId=' + e.currentTarget.dataset.orderId + '&actualPrice=' + e.currentTarget.dataset.actualPrice,
    })
  },
  //下拉刷新
  onPullDownRefresh() {
    this.reloadOrderList();
  },
  //页面显示
  onShow() {
    this.reloadOrderList();
  },
  //重新加载订单列表
  reloadOrderList(){
    var that = this;
    that.setData({
      orderList: [],
      page: 1,
      totalPages: 1
    });
    that.getOrderList();
  }
})